package org.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.com.pojo.UserBalance;
import org.com.service.UserBalanceService;
import org.com.mapper.UserBalanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
* @author 86134
* @description 针对表【user_balance】的数据库操作Service实现
* @createDate 2025-03-04 19:34:22
*/
@Service
public class UserBalanceServiceImpl extends ServiceImpl<UserBalanceMapper, UserBalance>
    implements UserBalanceService {

    @Autowired
    private UserBalanceMapper userBalanceMapper;

    @Override
    public BigDecimal getUserBalance(Integer userId) {
        QueryWrapper<UserBalance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        UserBalance userBalance = this.getOne(queryWrapper);
        return userBalance != null ? userBalance.getBalance() : BigDecimal.ZERO;
    }

    @Override
    public boolean initUserBalance(Integer userId) {
        UserBalance userBalance = new UserBalance();
        userBalance.setUserId(userId);
        userBalance.setBalance(BigDecimal.ZERO);
        userBalance.setCreatedAt(new Date());
        userBalance.setUpdatedAt(new Date());
        return this.save(userBalance);
    }

    @Override
    public int updateBalance(Long balanceId, BigDecimal balance, Date updatedAt) {
        return userBalanceMapper.updateBalance(balanceId, balance, updatedAt);
    }
}




